package com.an.cloud.controller;

import cn.hutool.core.bean.BeanUtil;
import com.an.cloud.entities.Pay;
import com.an.cloud.entities.PayDTO;
import com.an.cloud.resp.ResultData;
import com.an.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class PayController {
    @Resource
    private PayService payService;

    /**
     * 增加工资
     *
     * @param pay 支付
     * @return {@link String }
     * @author: ANHE
     * @createDate: 2024/03/13
     * @version: 1.0.0
     */
    @PostMapping("/pay/add")
    @Operation(summary = "新增", description = "新增支付流水方法,json串做参数")
    public ResultData<String> addPay(@RequestBody Pay pay) {
        System.out.println(pay.toString());
        int i = payService.add(pay);
        return ResultData.success("成功插入记录，返回值" + i);
    }

    /**
     * 删除支付
     *
     * @param id id
     * @return {@link Integer }
     * @author: ANHE
     * @createDate: 2024/03/13
     * @version: 1.0.0
     */
    @DeleteMapping("/pay/del/{id}")
    @Operation(summary = "删除", description = "删除支付流水方法")
    public ResultData<Integer> deletePay(@PathVariable("id") Integer id) {
        return ResultData.success(payService.delete(id));
    }

    /**
     * 更新支付
     *
     * @param payDTO 支付dto
     * @return {@link String }
     * @author: ANHE
     * @createDate: 2024/03/13
     * @version: 1.0.0
     */
    @PutMapping("/pay/update")
    @Operation(summary = "修改", description = "修改支付流水方法")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO) {
        Pay pay = new Pay();
        BeanUtil.copyProperties(payDTO, pay);
        int i = payService.update(pay);
        return ResultData.success("成功插入记录，返回值" + i);
    }

    /**
     * 按身份证领取
     *
     * @param id id
     * @return {@link Pay }
     * @author: ANHE
     * @createDate: 2024/03/13
     * @version: 1.0.0
     */
    @GetMapping("/pay/get/{id}")
    @Operation(summary = "按照ID查流水", description = "查询支付流水方法")
    public ResultData<Pay> getById(@PathVariable("id") Integer id) {
        try {
            TimeUnit.SECONDS.sleep(62);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (id < 0) {
            throw new RuntimeException("id不能为负数");
        }
        return ResultData.success(payService.getById(id));
    }

    /**
     * 得到所有
     *
     * @return {@link List }<{@link Pay }>
     * @author: ANHE
     * @createDate: 2024/03/13
     * @version: 1.0.0
     */
    @GetMapping("/pay/getAll")
    public ResultData<List<Pay>> getAll() {
        return ResultData.success(payService.getAll());
    }

    /**
     * 端口
     */
    @Value("${server.port}")
    private String port;

    /**
     * 通过consul获取信息
     *
     * @param anInfo 一个信息
     * @return {@link String }
     * @author: ANHE
     * @createDate: 2024/03/13
     * @version: 1.0.0
     */
    @GetMapping("/pay/get/info")
    public String getInfoByConsul(
            @Value("${an.info}") String anInfo) {
        return "anInfo from consul:" + anInfo + "\t" + "port: " + port;
    }
}
